﻿using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using LiveNet.Photos;
using LiveNet.Authentication;

public partial class _Default : System.Web.UI.Page 
{
    LivePhotos m_photos;
    protected void Page_Load(object sender, EventArgs e)
    {
        string DAT = "A:20080122T134905Z:dev.live.com:HmGgtO7L3wdlLbpsBQzSlKkaUPOvLc6SLJaLBdNhBrZYIBXCGXoCmYhT6zocmaqUBfWJzWxrX/YhE16PbBTdd3B++GG3Lkh2vGEXOu16kQ7MZ8eVFp0Q4fJfZX0mbPZD5FftA7eN4mScglQ2whS6UWzeLf75A3upFkCTy4ixV43c8hRlPESJbpDiTISQsup5w7A3KpL/fc9Lx7QnIDibOKnbOg2Ez3HBxUKPyR0MQ/CbbYGXSxY1fuKjFtKdvFQMWP+ySejdGrvsg+N805IOHN5yjaKNqwqHIInBUlO9EK/llnaoVT1gjAt8TOCd59VBwwFSmsruFR97z1BcoJdA1Q==";
        string userId = "livenet@live.co.uk";
        lblUserName.Text = string.Format("Signed in as: {0}", userId);
        m_photos = new LivePhotos(userId, DAT, AuthenticationToken.DomainAuthenticationToken);
        lblSuccess.Text = string.Empty;
    }
    protected void btnGetAlbums_Click(object sender, EventArgs e)
    {
        lbAlbums.Items.Clear();
        Album[] albums = m_photos.ListAlbums();
        foreach (Album album in albums)
        {
            ListItem item = new ListItem(album.Name, album.AlbumUrl.ToString());
            lbAlbums.Items.Add(item);
        }
    }
    protected void lbAlbums_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbPhotos.Items.Clear();
        lblPhotoLink.Text = string.Empty;
        imgPhoto.ImageUrl = string.Empty;
        imgPhoto.Visible = false;
        lblPhotoLink.Visible = false;
        Photo[] photos = m_photos.ListPhotos(new Uri(lbAlbums.SelectedItem.Value));
        foreach (Photo photo in photos)
        {
            ListItem item = new ListItem(photo.Name, photo.PhotoUrl.ToString());
            lbPhotos.Items.Add(item);
        }
    }
    protected void btnAddAlbum_Click(object sender, EventArgs e)
    {
        string name = txbxNewAlbum.Text;
        if (!string.IsNullOrEmpty(name))
        {
            Album newAlbum = m_photos.CreateAlbum(name);
            if (newAlbum != null)
            {
                lblSuccess.Text = string.Format("\"{0}\" created successfully.", newAlbum.Name);
            }
        }
        else
        {
            lblSuccess.Text = "There was an error adding the album.";
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Album album = m_photos.GetAlbum(new Uri(lbAlbums.SelectedItem.Value));
        if (album != null)
        {
            if (!string.IsNullOrEmpty(fileUpload.PostedFile.FileName))
            {
                Photo photo = album.AddPhoto(fileUpload.PostedFile.InputStream, fileUpload.FileName, "Added from LiveNet");
                if (photo != null)
                {
                    lblPhotoSuccess.Text = string.Format("\"{0}\" has been uploaded OK.", photo.Name);
                    lbAlbums_SelectedIndexChanged(this, null);
                }
            }
            else
            {

            }
        }
    }
    protected void btnDeletePhoto_Click(object sender, EventArgs e)
    {
        Photo photo = m_photos.GetPhoto(new Uri(lbPhotos.SelectedItem.Value));
        if (m_photos.DeletePhoto(photo))
        {
            lblDeleteSuccess0.Text = string.Format("\"{0}\" deleted successfully.", photo.Name);
            lblPhotoLink.Text = string.Empty;
            imgPhoto.ImageUrl = string.Empty;
            imgPhoto.Visible = false;
            lblPhotoLink.Visible = false;
            lbAlbums_SelectedIndexChanged(this, null);
        }
    }
    protected void btnDeleteAlbum_Click(object sender, EventArgs e)
    {
        Album album = m_photos.GetAlbum(new Uri(lbAlbums.SelectedItem.Value));
        if (m_photos.DeleteAlbum(album))
        {
            lblDeleteSuccess.Text = string.Format("\"{0}\" deleted successfully.", album.Name);
            btnGetAlbums_Click(this, null);
        }
    }
    protected void btnGetPhoto_Click(object sender, EventArgs e)
    {
        Photo photo = m_photos.GetPhoto(new Uri(lbPhotos.SelectedItem.Value));
        lblPhoto.Visible = true;
        lblPhotoLink.Text = photo.PhotoUrl.ToString();
        lblPhotoLink.Visible = true;
        imgPhoto.ImageUrl = string.Format("./photo.lnet?url={0}&oh={1}", photo.PhotoPath, photo.Owner.UserId);
        imgPhoto.Visible = true;
    }
}
